package com.atguigu.guliedu.banner.service.impl;

import com.atguigu.guliedu.banner.entity.Banner;
import com.atguigu.guliedu.banner.mapper.BannerMapper;
import com.atguigu.guliedu.banner.service.BannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author java
 * @since 2021-02-02
 */
@Service
@Slf4j
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    public Page<Banner> listBanner(Map<String, String> params) {
        Page<Banner> BannerPage = new Page<Banner>(Long.valueOf(params.get("page")),Long.valueOf(params.get("limit")));
        QueryWrapper<Banner> queryWrapper = new QueryWrapper<>();
        String key = params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.like("title",key);
        }
        String disabled = params.get("disabled");
        if(disabled.equals("true")||disabled.equals("false")){
            log.info("disabled->"+disabled);
            queryWrapper.eq("is_deleted",disabled.equals("true")?true:false);
        }
        IPage<Banner> BannerIPage = baseMapper.selectPage(BannerPage, queryWrapper);
        return (Page)BannerIPage;
    }

    @Override
    public List<Banner> getBanner() {
        List<Banner> list = baseMapper.selectList(new QueryWrapper<Banner>().eq("is_deleted", true));
        return list;
    }
}
